package com.test.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.test.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000132682378";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCYo6Ckd5PvvKT3Oip59DxmGhSLGnNDPnWbzqpeR2XhGacXLElFGmvuu41YoK4JALegR+5EYugmkE54S7yr/XKKdC95XkZ1LoPsucgeng4M9pFQMDfmcIbMsizMQyxSo4dSE6o2iqXEzoTljDlun2erLreJBYNCaxd5CMZALsEE6l/2QAOKZN2PgHixc4hQQzvvA4TgJ1KEOu0MRArtMbj2wa1qpYJFA6VFOYTO95FxCQfhrht6pK4a6bmhjfhARhzJ0Oy8iDf6jgvjpm69aA2OdcYii7Bp3rCJUNkjQd1M0xXfkbIB7iovzHe85QfhO1y4H5JzU0Mh44KEtoINIU63AgMBAAECggEAVykAf0nlCP2QubfWL4N+xdLYrkHxVZGh+NF/SYy9qVdLqZjZUgNvZIhmgxhlqLBnKrhIX3aXGAZJOLoOx6tXL6Z+wXBLDXj9Tj0cABfC8DYP7llKYmUZJGQN3qnw8lN83ypphPGRDRvcmXOEDB4sRXzMwwqJ1HBwm+Wdx2KrR5FoQE2mlj+QYtPMVunHBfFKfXafurzv1geHLok9DYO4p68otKaHlKcWjLmRo/Gskf2kmwHPfDJW3x5LGf9VeY4w2T5hJIb806cZg6xDPW5Eh9oM7qKOKuEvR5hU19iExCN6E5ejifP2HKueGFncfS3J/g7gPHuaftoTgr0EEE3KYQKBgQDNxAkFUlpo4UXcGYu+vDiaHLBVH9crm+8lnQ1hcF93nW/TMiKKOmvkIsABkCbLMLx7Fgwb37hs3kz9y5fJ3IiN/xrUxQWzBRY+J5NhiNh4l21RD1rGFgcpd9xH/ICL9eLtq/jAOj4c9pEazScDVIC4P0BmoSswhloVZD3ppE9w8QKBgQC950hF/8uj2DkLRXCYY32n3tX5CWbMMyUEo0n7qPBI6zaAJn+WiFkO02+uGVOGN7EjemyB7zOfny3PYLH2XyxToWDOzJjfVX+YFaJye/Jzc5Wy8cxYMzdUqRUYUpoFPvBxYE2umaVytwOQoAjZc/T6mQfmFZDmYRX63GABHHi6JwKBgQCfGPFkpP101KIAgdwUeVJGjuIkn37uoB4z9hAvhAS85wVcxgoA2RUtC1Vsn/nu59sETNLGBqCBxQzAu9e8ZAFN0a78Tu6OMeO14UHKi1XdQDBtGbM1MDkPbjbrjigSj+eLn4mxkBcQq/EcSP3eTEtOqtFvyTQsy+UBX0WjzsEB8QKBgHJKCXMnWVWIQfyIlBUCe5NWRwyUHevzM+owo8vzhnk8JhbGFK9x67dChMoQr/Xs+ozRlxAz96uvTxCprzEJYlIKo4qSE8cFW78Z9iRUeDwHaibB4yuJ11j6UZnLzKovkOGLBfV4vtvlTR3zUqAca2mOQ/SzMfuUnnfVvWmtyvKtAoGAX7RWkdPrgi9SR0kVVjLJy16CqiiK1XAPtN5K63VoTEXOJ0OjGjb3UUhT+3rNCbSslVt5tbbV2RmvU2zesOelSy4YOXjcplX017xx+ph+6WnuyJ2owFAyWOFo8A0jsUGnmj5LN8k4CW6o8zLeGAEaYGTQp8N2lmYBxBxMKi9dLHQ=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsQUjku6aW+4SzVzuK8tWV/7YjiINvfBFDsWgDUP94nktG2T9ay9lxKCcToHF69n56SMnN/wnyDuqIvSDfH3vos+zyBFo8z51tDO9vBjwRHWV33RE+1mNYvNSxGuvy1pH0W5mqjFxrPEwIG92j/yGhDYfLd+fMP0ze2W/KFkmQOBUCKJ3u48tbmwvrHcCzWLhsHGNJpz7sedkLVSdRS4jvGUkU3cFf80O6gUCUDaCW6HizVkgn0DVUrPQ7KpfngK47EX0/f5zNcu0ddBkDzK6NDJv7Gt+wETxlgX2IWHFKa8Ye5DSHLEC79y/wnQOR5h1iIk3UVyejJ00vZikEepXMwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //超时时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
